﻿using TravelServer.Dtos;
using TravelServer.Models;

namespace TravelServer.Services
{
    public class PropertyMappingService : IPropertyMappingService
    {
        public bool IsMappingExists<TSource, TDestination>(string fields)
        {
            var propertyMapping = GetPropertyMapping<TSource, TDestination>();

            if (string.IsNullOrWhiteSpace(fields))
            {
                return true;
            }

            //逗号来分隔字段字符串
            var fieldsAfterSplit = fields.Split(",");

            foreach (var field in fieldsAfterSplit)
            {
                // 去掉空格
                var trimmedField = field.Trim();
                // 获得属性名称字符串
                var indexOfFirstSpace = trimmedField.IndexOf(" ");
                var propertyName = indexOfFirstSpace == -1 ?
                    trimmedField : trimmedField.Remove(indexOfFirstSpace);

                if (!propertyMapping.ContainsKey(propertyName))
                {
                    return false;
                }
            }
            return true;
        }
        public PropertyMappingService()
        {
            propertyMappings.Add(new PropertyMapping<TouristRouteDto, TouristRoute>(touristRountePropertyMapping));
        }
        //构建一个旅游路线属性映射字典,属性名称与DTO或Model一致
        private Dictionary<string, PropertyMappingValue> touristRountePropertyMapping =
            new Dictionary<string, PropertyMappingValue>(StringComparer.OrdinalIgnoreCase)
            {
                {"Id",new PropertyMappingValue(new List<string>(){"Id"}) },
                {"Title",new PropertyMappingValue(new List<string>(){"Title"}) },
                {"Rating",new PropertyMappingValue(new List<string>(){"Rating"}) },
                {"OriginalPrice",new PropertyMappingValue(new List<string>(){"OriginalPrice"}) },
            };

        private IList<IPropertyMapping> propertyMappings = new List<IPropertyMapping>();
        public Dictionary<string, PropertyMappingValue> GetPropertyMapping<TSource, TDestination>()
        {
            //通过传入的泛型，获取匹配映射对象
            var matchingMapping = propertyMappings.OfType<PropertyMapping<TSource, TDestination>>();
            if (matchingMapping != null && matchingMapping.Count() == 1)
            {
                return matchingMapping.First().mappingDic;
            }
            throw new Exception($"Not Find{nameof(TSource)}and{nameof(TDestination)}");
        }
    }
    public class PropertyMappingValue
    {
        public IEnumerable<string> DestinationProperties { get; private set; }
        public PropertyMappingValue(IEnumerable<string> DestinationProperties)
        {
            this.DestinationProperties = DestinationProperties;
        }
    }

    public class PropertyMapping<TSource, TDestination>:IPropertyMapping
    {
        public Dictionary<string, PropertyMappingValue> mappingDic =new Dictionary<string, PropertyMappingValue>();
        public PropertyMapping(Dictionary<string, PropertyMappingValue> mappingDic)
        {
            this.mappingDic = mappingDic;
        }
    }
}